
#include <QFile>
#include <QFileDialog>
#include <QTabWidget>

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mpCurrDocument(0)
{
    ui->setupUi(this);

    mpDomTreeWidget = new DomTreeWidget(this);
    ui->leftDockWidget->setWidget(mpDomTreeWidget);

    QTabWidget *tabWidget = new QTabWidget(this);
    setCentralWidget(tabWidget);

    mpDomElementEdit = new DomElementEdit(this);
    tabWidget->addTab(mpDomElementEdit, "Element Editor");

    connect(mpDomTreeWidget, SIGNAL(nodeSelected(const QDomNode&)), mpDomElementEdit, SLOT(setDomNode(const QDomNode&)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initToolsBars()
{
    // Add open action to toolbar
    ui->mainToolBar->addAction(ui->actionOpen);
}


void MainWindow::on_actionOpen_triggered()
{
    QString filter = ""; //XML files (*.xml);; All Files (*.*)";
    QString fName = QFileDialog::getOpenFileName(0, "Open XML file",
                                                     QDir::currentPath(), filter);
    if(fName.isEmpty())
        //return;
     fName = "D:/Programmierung/XmlEdit/mainwindow.ui";
    QFile file(fName);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug("Could not open file");
    }
    else
    {
        QDomDocument *document = new QDomDocument("document");
        QString text = file.readAll();
        if(!document->setContent(text))
        {
            qDebug("Parsing failed");
        }
        else
        {
            mpDomTreeWidget->setRootNode(*document);//->childNodes().at(1));
        }
    }
}
